<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class template_process extends Admin_Controller
{
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template.template_list_model');
		$col = $CI->uri->segment($CI->template_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->template_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->template_list_model->cache->add($k, $order[$CI->template_list_model->cache->load($k)]);
			} else
				$CI->template_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->template_list_model->url_prefix.'/'.$CI->template_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template.template_list_model');
		
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				$CI->db->delete(TEMPLATES, array(TEMPLATES_TEMPLATE_TYPE_ID=>$id, TEMPLATES_FRANCHISE_ID=>$CI->template_list_model->admin_id()));			
			}

			// message
			$CI->template_list_model->cache->add('msg', $CI->lang->line('list_delete_all'));
		} else
			// message
			$CI->template_list_model->cache->add('msg', $CI->lang->line('list_cannot_delete'));
		
		// go to original page
		redirect($CI->template_list_model->url_prefix.'/'.$CI->template_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template.template_list_model');
		
		$id = $CI->uri->segment($CI->template_list_model->item_segment);
		$CI->db->delete(TEMPLATES, array(TEMPLATES_TEMPLATE_TYPE_ID=>$id, TEMPLATES_FRANCHISE_ID=>$CI->template_list_model->admin_id()));
		
		// message
		$CI->template_list_model->cache->add('msg', $CI->lang->line('list_delete'));
		
		// go to original page
		redirect($CI->template_list_model->url_prefix.'/'.$CI->template_list_model->start);
	}	
	
	function saveItem()
	{
		global $CI;
		
		// model
		$CI->load->module_model('template.template_detail_model');
		
		// validate
		$CI->load->library('form_validation');
        //$CI->form_validation->set_rules('subject', 'Subject', 'trim|required');
		$CI->form_validation->set_rules('body', 'Body', 'trim|required');
        if ($CI->form_validation->run() == FALSE)
		{
			// message
			$CI->template_detail_model->cache->add('msg', $CI->lang->line('detail_cannot_save')." : ".$CI->form_validation->error_string());
			$CI->template_detail_model->cache->add('subject', $CI->input->post('subject'));
			$CI->template_detail_model->cache->add('body', $CI->input->post('body'));
			$CI->template_detail_model->cache->add('template_type_id', $CI->input->post('template_type_id'));

			// go to original page
			redirect($CI->template_detail_model->url_prefix.'/'.$CI->template_detail_model->start.'/'.$CI->template_detail_model->item);
		}
		
		// save to template
		if ($CI->template_detail_model->hasTemplate())
		{
			// update
			$sql = "UPDATE ".TEMPLATES." SET";
			$sql .= " ".TEMPLATES_SUBJECT."=".$CI->db->escape($CI->input->post('subject'));
			$sql .= ", ".TEMPLATES_BODY."=".$CI->db->escape($CI->input->post('body'));
			$sql .= ", ".TEMPLATES_TEMPLATE_TYPE_ID."=".$CI->input->post('template_type_id');
			$sql .= " WHERE ".TEMPLATES_FRANCHISE_ID."=".$CI->template_detail_model->admin_id();
			$sql .= " AND ".TEMPLATES_TEMPLATE_TYPE_ID."=".$CI->template_detail_model->item;
			$CI->db->query($sql);
		} else {
			// insert
			$data = array(
				TEMPLATES_SUBJECT=>$CI->input->post('subject'),
				TEMPLATES_BODY=>$CI->input->post('body'),
				TEMPLATES_TEMPLATE_TYPE_ID=>$CI->input->post('template_type_id'),
				TEMPLATES_FRANCHISE_ID=>$CI->template_detail_model->admin_id(),
			);
			$CI->db->insert(TEMPLATES, $data);
		}

		// message
		$CI->template_detail_model->cache->add('msg', $CI->lang->line('detail_save'));
		
		// go to original page
		redirect($CI->template_detail_model->url_prefix2.'/'.$CI->template_detail_model->start);
	}		
}

?>